package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.PurchaseOrderProduct;
import com.fowo.api.model.purchase.order.PurchaseOrderPushInStorageAction;
import com.fowo.api.model.purchase.order.PurchaseOrderVo;
import com.fowo.api.model.stock.in.StockInVo;
import com.fowo.api.model.stock.in.product.StockInProductItemVo;
import com.fowo.api.service.PurchaseOrderProductService;
import com.fowo.api.service.PurchaseOrderService;
import com.fowo.api.service.StockInProductService;
import com.fowo.api.service.impl.PurchaseOrderProductServiceImpl;
import com.fowo.api.service.impl.PurchaseOrderServiceImpl;
import com.fowo.api.service.impl.StockInServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class purchaseOrderPushInStorage implements PurchaseOrderPushInStorageAction {

    @Resource
    private PurchaseOrderServiceImpl purchaseOrderService;
    @Resource
    protected PurchaseOrderProductServiceImpl purchaseOrderProductService;
    @Resource
    private StockInServiceImpl stockInService;
    @Resource
    protected StockInProductService stockInProductService;

    @Override
    public void run(Long id) throws Exception {
        PurchaseOrderVo voById = purchaseOrderService.getVoById(id);
        Assert.notNull(voById,"没找到这个订单");
        Assert.isTrue(voById.getAnStatus().equals("9"),"该订单没有审核!");
        List<PurchaseOrderProduct> purchaseOrderList = purchaseOrderProductService.list(Wrappers.<PurchaseOrderProduct>query().lambda().eq(PurchaseOrderProduct::getPurchaseOrderId, id));
        AddStockInVo(voById,purchaseOrderList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void AddStockInVo(PurchaseOrderVo voById, List<PurchaseOrderProduct> purchaseOrderList) {
        StockInVo model =new StockInVo();
        model.setStatus("0");
        model.setAStatus("committed");
//        model.setRemarks(voById.getRemarks());
        model.setApplicationDep(voById.getApplicationDep());
        model.setApplicationDepName1(voById.getApplicationDepName1());
        model.setReceiptType("RK002");
        model.setProductType(voById.getProductType());
        model.setWarehouse(voById.getWid());
        model.setNeedOrg(voById.getOrgId());
        model.setAddType(voById.getProductType());
        model.setRemarks(voById.getRemarks()==null?"采购订单:"+voById.getPurchaseOrderCode():voById.getRemarks()+",采购订单:"+voById.getPurchaseOrderCode());
        model.setWarehousingDate(voById.getPurchasesDate());
        model.setSupplier(voById.getSupplierId());
        model.setWarehousingDate(new Date());
        model.setGenerateAp("0");
        model.setCgUid(voById.getOptUid());
        model.setAddType("0");//设置原材料的
        model.setPurchaseOrderCode(voById.getPurchaseOrderCode());
        model.setPurchasingOrg(voById.getPurchasesOrg());//getPurchaseOrderCode
        model.setNeedOrg(voById.getSettlementOrg());
        List<StockInProductItemVo> productList=new ArrayList<>();
        for(int i=0;i<purchaseOrderList.size();i++){
            StockInProductItemVo stockInProductItemVo=new StockInProductItemVo();
            PurchaseOrderProduct purchaseOrderProduct = purchaseOrderList.get(i);
            stockInProductItemVo.setRemarks(purchaseOrderProduct.getRemarks());
            stockInProductItemVo.setProductId(purchaseOrderProduct.getProductId());
            stockInProductItemVo.setUnitStock(purchaseOrderProduct.getApplicationNum());
            stockInProductItemVo.setProductName(purchaseOrderProduct.getProductName());
            stockInProductItemVo.setQuantity(purchaseOrderProduct.getApplicationNum());
            stockInProductItemVo.setGoodProduct(purchaseOrderProduct.getApplicationNum());
            stockInProductItemVo.setSku(purchaseOrderProduct.getSku());
            stockInProductItemVo.setUnitPrice(purchaseOrderProduct.getUnitPrice());
            stockInProductItemVo.setCostPrice(purchaseOrderProduct.getCostPrice());
            stockInProductItemVo.setMainUnitPrice(purchaseOrderProduct.getMainUnitPrice());
            stockInProductItemVo.setGramWeight(purchaseOrderProduct.getGramWeight());
            stockInProductItemVo.setLaborCosts(purchaseOrderProduct.getLaborCosts());
            stockInProductItemVo.setAccessoriesPrice(purchaseOrderProduct.getAccessoriesPrice());
            stockInProductItemVo.setSubtotal(purchaseOrderProduct.getTotalPrice());
            productList.add(stockInProductItemVo);
        }
        model.setProductList(productList);
        try {
            stockInService.create(model);
        } catch (Exception e) {
            throw new RuntimeException("");
        }
    }
}
